﻿using UnityEngine;
using System.Collections;

public class JoyStickController : MonoBehaviour {

    private bool isPress = false;

    private Transform button;

    // 加上静态方便调用
    public static float h;
    public static float v;
    // UI的Collider才生效
    void OnPress(bool isPress)
    {
        // 点击事件
        this.isPress = isPress;
        if (isPress == false)  // 等于false时 为松开
        {
            button.localPosition = Vector3.zero;  // 返回原点
            h = 0;
            v = 0;
        }

    }

    void Awake()
    {
        button = transform.Find("Button");
    }

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {

        if (isPress)
        {
            Vector2 touchPos= UICamera.lastTouchPosition; // 获得游戏屏幕的像素点坐标
             touchPos -= new Vector2(91,91); //把Button的中心点作为原点(0,0)

            float distance = Vector2.Distance(Vector2.zero,touchPos);
            if (distance < 73)
            {
                button.localPosition = touchPos; //将鼠标在屏幕上的像素点坐标传递给Button的像素点坐标
            }
            else
            {
                // normalized单位化唯一 归一化
                touchPos = touchPos.normalized * 73;
                button.localPosition = touchPos;
            }

           h = touchPos.x / 73;
           v = touchPos.y / 73;


        }
	}
}
